package com.wdc.回溯;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class E03Leetcode17 {
    List<String> result = new ArrayList<>();

    public List<String> letterCombinations(String digits) {
        if (digits == null || digits.length() == 0){
            return result;
        }
        //做一个映射
        String[] numString = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        backtracking(digits,numString,0);
        return result;

    }
    StringBuilder temp = new StringBuilder();

    /**
     *
     * @param digits 数字字符串
     * @param numString  映射表
     * @param index 当前索引
     */
    void backtracking(String digits ,String[] numString , int index){
        //结束条件
        if (index == digits.length()){
            result.add(temp.toString());
            return;
        }
        //获取当前的数字
        String str = numString[digits.charAt(index) - '0'];
        for (int i = 0; i <str.length(); i++ ){
            //处理节点
            temp.append(str.charAt(i));
            //递归调用
            backtracking(digits,numString,index + 1);
            //回溯
            temp.deleteCharAt(temp.length() - 1);
        }

    }



}
